package cn.edu.bupt.thymeleaf.examples.filter;

import cn.edu.bupt.thymeleaf.examples.entities.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;


public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        // 检查session中是否有用户信息
        Object user = request.getSession(true).getAttribute("user");
        if (user == null) {
            // 用户未登录，重定向到登录页面
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }
}
